package com.gmgx.controller;

import com.gmgx.entity.Student;
import com.gmgx.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes({"msg"})
@RequestMapping("student")
public class StudentController {
    @Autowired
    private IStudentService studentService;

    // 报错 405：方法不允许 Request method 'GET' not supported
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public ModelAndView register(Student student){
        System.out.println(student);
        ModelAndView mv = new ModelAndView();
        boolean flag = studentService.register(student);
        // 注册成功
        if (flag){
            mv.addObject("msg", "注册成功，您的学号为：" + student.getSno());
            mv.setViewName("redirect:../main.jsp");
        }else { // 注册失败
            mv.addObject("msg", "注册失败，请重新注册");
            mv.setViewName("register");
        }
        return mv;
    }
}
